package com.rabbit.controller;

import com.github.pagehelper.PageInfo;
import com.rabbit.annotation.Log;
import com.rabbit.annotation.LoginUser;
import com.rabbit.constant.OperType;
import com.rabbit.db.entity.User;
import com.rabbit.exception.BusinessException;
import com.rabbit.service.DictService;
import com.rabbit.util.ResultGenUtil;
import com.rabbit.validate.base.Delete;
import com.rabbit.validate.base.Enable;
import com.rabbit.validate.base.Insert;
import com.rabbit.validate.base.Update;
import com.rabbit.vo.ResultVO;
import com.rabbit.vo.req.DictReq;
import com.rabbit.vo.res.DictDataRes;
import com.rabbit.vo.res.DictRes;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <h1>字典管理</h1>
 *
 * @author zjw
 * @date 2022/1/17 16:51
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Resource
    private DictService dictService;

    /**
     * 根据dictType查询对应字典
     */
    @GetMapping("/queryDictData")
    public ResultVO<Object> queryDictData(@RequestParam String dictTypes) {
        List<DictDataRes> dictList = dictService.queryDictData();
        List<String> dictTypeList = Arrays.asList(dictTypes.split(","));
        dictList = dictList.stream().filter(e -> dictTypeList.contains(e.getDictType())).collect(Collectors.toList());
        return ResultGenUtil.genSuccess(dictList);
    }

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResultVO<Object> list(@RequestBody DictReq dictReq) {
        PageInfo<DictRes> pageInfo = dictService.queryDictList(dictReq);
        return ResultGenUtil.genSuccess(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 删除字典
     */
    @PostMapping("/delete")
    @Log(name = "字典管理", type = OperType.DELETE)
    public ResultVO<Object> deleteDict(@Validated(Delete.class) @RequestBody DictReq dictReq) {
        dictService.deleteDict(dictReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 调整字典状态
     */
    @PostMapping("/changeEnabled")
    @Log(name = "字典管理", type = OperType.UPDATE)
    public ResultVO<Object> changeEnabled(@LoginUser User loginUser, @Validated(Enable.class) @RequestBody DictReq dictReq) {
        dictReq.setOperatorUserName(loginUser.getUserName());
        dictService.updateDictEnabled(dictReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 查询指定字典
     */
    @GetMapping("/queryDictById")
    public ResultVO<Object> queryDictById(@RequestParam Long dictId) {
        return ResultGenUtil.genSuccess(dictService.queryDictById(dictId));
    }

    /**
     * 新增字典
     */
    @PostMapping("/add")
    @Log(name = "字典管理", type = OperType.INSERT)
    public ResultVO<Object> addDict(@LoginUser User loginUser, @Validated(Insert.class) @RequestBody DictReq dictReq) throws BusinessException {
        dictService.checkUnique(dictReq, 1);
        dictReq.setOperatorUserName(loginUser.getUserName());
        dictService.addDict(dictReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 修改字典
     */
    @PostMapping("/update")
    @Log(name = "字典管理", type = OperType.UPDATE)
    public ResultVO<Object> updateDict(@LoginUser User loginUser, @Validated(Update.class) @RequestBody DictReq dictReq) throws BusinessException {
        dictService.checkUnique(dictReq, 2);
        dictReq.setOperatorUserName(loginUser.getUserName());
        dictService.updateDict(dictReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 查询所有可用的字典
     */
    @GetMapping("/loadAllDict")
    public ResultVO<Object> loadAllDict() {
        return ResultGenUtil.genSuccess(dictService.queryAllDict());
    }
}
